package com.ruiysoft.conf;

import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultCode;
import com.ruiysoft.pub.core.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.Set;

/**
 * 全局异常捕获，其他异常自行添加，并配置code
 */
@ControllerAdvice
public class DefualtExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(Thread.currentThread().getClass());


    /**
     * 拦截非法参数异常
     *
     * @author
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public Result interceptServicelException(IllegalArgumentException e) {
        logger.error("IllegalArgumentException",e);
        return ResultGenerator.genExceptionResult(ResultCode.CODE_ILLEGALARGUMENT_ERROR,e.getMessage());
    }


    /**
     * 拦截未知的运行时异常
     *
     * @author
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result interceptException(Exception e) {
        logger.error("interceptException",e);
        return ResultGenerator.genFailResult(e.getMessage());
    }

    /*参数异常处理*/
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    @ResponseStatus
    public Result interceptValidationException(ConstraintViolationException exception) {
        logger.error("ConstraintViolationException",exception);
        Set<ConstraintViolation<?>> violations = exception.getConstraintViolations();
        for (ConstraintViolation<?> item : violations) {
            logger.debug(item.getMessage());
            return ResultGenerator.genExceptionResult(ResultCode.CODE_VALIDATION_ERROR,item.getMessage());
        }
        return ResultGenerator.genExceptionResult(ResultCode.CODE_VALIDATION_ERROR,violations.toString());
    }



    /* RequestParam 入参异常 */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    @ResponseStatus
    public Result MissingServletRequestParameterExceptionException(MissingServletRequestParameterException exception) {
        logger.error("MissingServletRequestParameterExceptionException",exception);
        return ResultGenerator.genExceptionResult(ResultCode.CODE_MISS_ERROR,exception.getMessage());
    }


    /* NullPointerException 入参异常 */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    @ResponseStatus
    public Result NullPointerExceptionException(NullPointerException exception) {
        logger.error("NullPointerExceptionException",exception);
        return ResultGenerator.genFailResult("服务异常");
    }

}
